﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace SQLTask2
{
    public partial class RatesForm : Form
    {
        private MySqlConnection pConn;
        private MySqlDataAdapter da = null;
        private DataTable ds = null;

        private string[] queries = {//Fill main grid
                                    @"SELECT Rate_ID AS `ID`,
                                             Buy AS `Покупка`,
                                             Sell AS `Продажа`,
                                             Date AS `Дата`,
                                             f.Name AS `В1`,
                                             s.Name AS `В2`
                                     FROM rates
                                     JOIN currencies f ON F_Currency_ID = f.Currency_ID
                                     JOIN currencies s ON S_Currency_ID = s.Currency_ID",
                                     @"SELECT Rate_ID AS `ID`,
                                             Buy AS `Покупка`,
                                             Sell AS `Продажа`,
                                             Date AS `Дата`,
                                             f.Name AS `В1`,
                                             s.Name AS `В2`
                                     FROM rates
                                     JOIN currencies f ON F_Currency_ID = f.Currency_ID
                                     JOIN currencies s ON S_Currency_ID = s.Currency_ID
                                     WHERE rates.Date BETWEEN '{0}' AND '{1}'",
                                   };






        public RatesForm(MySqlConnection conn)
        {
            InitializeComponent();
            pConn = conn;
            ds = new DataTable();
        }


        private void UpdateMainTable()
        {
            try
            {
                ds.Clear();
                da.Fill(ds);
                dataGridView1.Refresh();
            }
            catch (MySqlException sqlExc)
            {
                MessageBox.Show(sqlExc.Message);
            }
        }


        private void RatesForm_Load(object sender, EventArgs e)
        {
            try
            {
                da = new MySqlDataAdapter(queries[0], pConn);
                dataGridView1.DataSource = ds;
                UpdateMainTable();
            }
            catch (MySqlException sqlExc)
            {
                MessageBox.Show(sqlExc.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AddRateForm frm = new AddRateForm(pConn);
            frm.ShowDialog();
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            panel1.Enabled = !panel1.Enabled;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if(radioButton2.Checked)
                {
                    da = new MySqlDataAdapter(String.Format(queries[1],
                                              dateTPFrom.Value.ToString("yyyy-MM-dd"),
                                              dateTPTo.Value.ToString("yyyy-MM-dd")), pConn);
                }
                else
                {
                    da = new MySqlDataAdapter(queries[0], pConn);
                }
                dataGridView1.DataSource = ds;
                UpdateMainTable();
            }
            catch (MySqlException sqlExc)
            {
                MessageBox.Show(sqlExc.Message);
            }
        }
    }
}
